package goodbaby.dklt.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.classic.core.activity.BaseActivity;
import com.classic.core.utils.ToastUtil;
import com.classic.okhttp.gbb.http.ActionHelp;
import com.classic.okhttp.gbb.http.ObjectCallback;
import com.google.gson.reflect.TypeToken;
import com.tgd.gbb.uikit.ui.dialog.DialogMaker;

import org.w3c.dom.Text;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import goodbaby.dklt.R;
import goodbaby.dklt.adapter.CommonAdapter;
import goodbaby.dklt.adapter.ViewHolder;
import goodbaby.dklt.bean.KeywordInfo;
import goodbaby.dklt.bean.ShutupInfo;

/**
 * Created by Li.z on 2016/5/7.
 * 敏感词设置
 */
public class KeywordActivity extends BaseActivity {
    public static final String TAG = "KeywordActivity";

    private TextView tvTitle;
    private ImageView ivBack;

    private TextView tvWord;
    private TextView tvShutup;
    private ViewPager viewPager;

    private View wordView;
    private View shutupView;

    private ListView lvShutup;

    private EditText etInputWord;
    private TextView btnAdd;
    private ListView lvWord;

    private KeywordAdapter mKeywordAdapter;
    private ShutupAdapter mShutupAdapter;

    @Override
    public int getLayoutResId() {
        return R.layout.activity_word;
    }

    @Override
    public void initView() {
        super.initView();
        tvTitle = (TextView) findViewById(R.id.tv_common_title);
        tvTitle.setText("敏感词设置");
        ivBack = (ImageView) findViewById(R.id.iv_common_back);
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(this);

        tvWord = (TextView) findViewById(R.id.tv_word);
        tvWord.setOnClickListener(this);
        tvShutup = (TextView) findViewById(R.id.tv_shutup);
        tvShutup.setOnClickListener(this);


        viewPager = (ViewPager) findViewById(R.id.vp_word);

        initWordView();

        initShutupView();

        viewPager.setAdapter(new ViewPagerAdapter());

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tvWord.setEnabled(!(position == 0));
                tvShutup.setEnabled(!(position == 1));
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        viewPager.setCurrentItem(0);
    }

    private void initShutupView() {
        shutupView = View.inflate(this, R.layout.fragment_shutup_list, null);
        lvShutup = (ListView) shutupView.findViewById(R.id.lv_listview);

        loadShutupData();
    }

    private void loadShutupData() {

        ActionHelp.getShutupList(activity, new ObjectCallback<ArrayList<ShutupInfo>>() {

            @Override
            public void onError(int code) {
                if (code == 301) {
                    Intent intent = new Intent(KeywordActivity.this, LoginActivity.class);
                    startActivity(intent);
                } else {
                    ToastUtil.showToast(activity, "失败:" + code);
                }
                Log.i(TAG, "请求错误" + code + ";" + code);
            }

            @Override
            public void onSuccess(ArrayList<ShutupInfo> response) {
                Log.i(TAG, response.toString());
                mShutupAdapter = new ShutupAdapter(KeywordActivity.this, response);
                lvShutup.setAdapter(mShutupAdapter);
            }

            @Override
            public Type getType() {
                return new TypeToken<ArrayList<ShutupInfo>>() {
                }.getType();
            }

            @Override
            public void onError(int code, String errorMessage) {
                super.onError(code, errorMessage);
                Log.i(TAG, "请求错误" + code + ";" + errorMessage);
                ToastUtil.showToast(activity, "请求错误" + code);
            }

            @Override
            public void onHttpError(int statusCode) {
                super.onHttpError(statusCode);
                ToastUtil.showToast(activity, "请求超时");
            }
        });

    }

    private void initWordView() {
        wordView = View.inflate(this, R.layout.fragment_word, null);

        etInputWord = (EditText) wordView.findViewById(R.id.et_input_word);
        btnAdd = (TextView) wordView.findViewById(R.id.btn_add);
        lvWord = (ListView) wordView.findViewById(R.id.lv_word);

        btnAdd.setOnClickListener(this);

        loadWordData();
    }


    /**
     * 加载keyword列表
     */
    private void loadWordData() {
        ActionHelp.getKeywordList(activity, new ObjectCallback<ArrayList<KeywordInfo>>() {

            @Override
            public void onError(int code) {
                if (code == 301) {
                    Intent intent = new Intent(KeywordActivity.this, LoginActivity.class);
                    startActivity(intent);
                } else {
                    ToastUtil.showToast(activity, "失败:" + code);
                }
                Log.i(TAG, "请求错误" + code + ";" + code);
            }

            @Override
            public void onSuccess(ArrayList<KeywordInfo> response) {
                Log.i(TAG, response.toString());
                mKeywordAdapter = new KeywordAdapter(KeywordActivity.this, response);
                lvWord.setAdapter(mKeywordAdapter);
            }

            @Override
            public Type getType() {
                return new TypeToken<ArrayList<KeywordInfo>>() {
                }.getType();
            }

            @Override
            public void onError(int code, String errorMessage) {
                super.onError(code, errorMessage);
                Log.i(TAG, "请求错误" + code + ";" + errorMessage);
                ToastUtil.showToast(activity, "请求错误" + code);
            }

            @Override
            public void onHttpError(int statusCode) {
                super.onHttpError(statusCode);
                ToastUtil.showToast(activity, "请求超时");
            }
        });
    }

    @Override
    public void viewClick(View v) {
        super.viewClick(v);
        switch (v.getId()) {
            case R.id.iv_common_back:
                finish();
                break;
            case R.id.btn_add:
                toAddKeyword();
                break;
            case R.id.tv_word:
                viewPager.setCurrentItem(0);
                break;
            case R.id.tv_shutup:
                viewPager.setCurrentItem(1);
                break;
        }
    }

    /**
     * 添加关键词
     */
    private void toAddKeyword() {
        final String keywork = etInputWord.getText().toString().trim();
        if (TextUtils.isEmpty(keywork)) {
            ToastUtil.showToast(this, "关键词不能为空！");
            return;
        }
        DialogMaker.showProgressDialog(this, "正在添加...");
        ActionHelp.addKeyword(activity, keywork, new ObjectCallback<Integer>() {

            @Override
            public void onError(int code) {
                if (code == 301) {
                    Intent intent = new Intent(KeywordActivity.this, LoginActivity.class);
                    startActivity(intent);
                } else {
                    ToastUtil.showToast(activity, "失败:" + code);
                    Log.i(TAG, "请求错误" + code + ";" + code);
                    DialogMaker.dismissProgressDialog();
                }
            }

            @Override
            public void onSuccess(Integer response) {
                Log.i(TAG, response.toString());
                mKeywordAdapter.addData(new KeywordInfo(keywork, response));
                ToastUtil.showToast(activity, "添加成功！");
                DialogMaker.dismissProgressDialog();
                etInputWord.setText("");
            }

            @Override
            public Type getType() {
                return new TypeToken<Integer>() {
                }.getType();
            }

            @Override
            public void onError(int code, String errorMessage) {
                super.onError(code, errorMessage);
                Log.i(TAG, "请求错误" + code + ";" + errorMessage);
                ToastUtil.showToast(activity, "请求错误" + code);
                DialogMaker.dismissProgressDialog();
            }

            @Override
            public void onHttpError(int statusCode) {
                super.onHttpError(statusCode);
                ToastUtil.showToast(activity, "请求超时");
                DialogMaker.dismissProgressDialog();
            }
        });

    }

    /**
     * 删除关键词
     */
    private void toDeleteKeyword(final KeywordInfo info) {

        DialogMaker.showProgressDialog(this, "正在删除...");
        ActionHelp.deleteKeyword(activity, info.getId(), new ObjectCallback<Object>() {

            @Override
            public void onError(int code) {
                if (code == 301) {
                    Intent intent = new Intent(KeywordActivity.this, LoginActivity.class);
                    startActivity(intent);
                } else {
                    ToastUtil.showToast(activity, "失败:" + code);
                    DialogMaker.dismissProgressDialog();
                }
            }

            @Override
            public void onSuccess(Object response) {
                Log.i(TAG, response.toString());

                mKeywordAdapter.removeData(info);
                ToastUtil.showToast(activity, "删除成功！");
                DialogMaker.dismissProgressDialog();
            }

            @Override
            public Type getType() {
                return new TypeToken<Object>() {
                }.getType();
            }

            @Override
            public void onError(int code, String errorMessage) {
                super.onError(code, errorMessage);
                Log.i(TAG, "请求错误" + code + ";" + errorMessage);
                ToastUtil.showToast(activity, "请求错误" + code);
                DialogMaker.dismissProgressDialog();
            }

            @Override
            public void onHttpError(int statusCode) {
                super.onHttpError(statusCode);
                ToastUtil.showToast(activity, "请求超时");
                DialogMaker.dismissProgressDialog();
            }
        });

    }

    private void toCancelShutup(final ShutupInfo item) {
        DialogMaker.showProgressDialog(this, "正在解除禁言...");
        ActionHelp.cancelShutup(activity, item.getUserid(), item.getId(), new ObjectCallback() {

            @Override
            public void onError(int code) {
                Log.i(TAG, "请求错误" + code + ";" + code);
                if (code == 301) {
                    Intent intent = new Intent(KeywordActivity.this, LoginActivity.class);
                    startActivity(intent);
                } else {
                    ToastUtil.showToast(activity, "失败:" + code);
                    DialogMaker.dismissProgressDialog();
                }
            }

            @Override
            public void onSuccess(Object response) {
                Log.i(TAG, response.toString());

                mShutupAdapter.removeData(item);
                ToastUtil.showToast(activity, "删除成功！");
                DialogMaker.dismissProgressDialog();
            }

            @Override
            public Type getType() {
                return new TypeToken<Object>() {
                }.getType();
            }

            @Override
            public void onError(int code, String errorMessage) {
                super.onError(code, errorMessage);
                Log.i(TAG, "请求错误" + code + ";" + errorMessage);
                ToastUtil.showToast(activity, "请求错误" + code);
                DialogMaker.dismissProgressDialog();
            }

            @Override
            public void onHttpError(int statusCode) {
                super.onHttpError(statusCode);
                ToastUtil.showToast(activity, "请求超时");
                DialogMaker.dismissProgressDialog();
            }
        });
    }

    private class ViewPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            if (position == 0) {
                container.addView(wordView, 0);
                return wordView;
            } else if (position == 1) {
                container.addView(shutupView, 1);
                return shutupView;
            }
            return null;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // super.destroyItem(container, position, object);
        }
    }

    private class KeywordAdapter extends CommonAdapter<KeywordInfo> {

        public KeywordAdapter(Context context, List<KeywordInfo> mDatas) {
            super(context, R.layout.item_list_word, mDatas);
        }

        @Override
        public void convert(ViewHolder helper, KeywordInfo item, int position) {
            helper.setText(R.id.tv_position, position + 1 + "");
            helper.setText(R.id.tv_word, item.getKeyword());

            TextView tvDelete = helper.getView(R.id.tv_delete);
            tvDelete.setTag(item);
            tvDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    KeywordInfo item = (KeywordInfo) v.getTag();
                    toDeleteKeyword(item);
                }
            });

        }
    }

    private class ShutupAdapter extends CommonAdapter<ShutupInfo> {

        public ShutupAdapter(Context context, List<ShutupInfo> mDatas) {
            super(context, R.layout.item_list_word, mDatas);
        }

        @Override
        public void convert(ViewHolder helper, ShutupInfo item, int position) {
            helper.setText(R.id.tv_position, position + 1 + "");
            helper.setText(R.id.tv_word, TextUtils.isEmpty(item.getChildrenName()) ? "老师" : item.getChildrenName());

            TextView tvDelete = helper.getView(R.id.tv_delete);
            tvDelete.setText("解除禁言");
            tvDelete.setTag(item);
            tvDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ShutupInfo item = (ShutupInfo) v.getTag();
                    //toDeleteKeyword(item);
                    //解除禁言
                    toCancelShutup(item);
                }
            });

        }
    }
}
